package com.ruoyi.fantasy.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huanzhidadi.fantasy.bean.domain.FantasyCity;
import com.huanzhidadi.fantasy.bean.domain.FantasyCityMap;
import com.huanzhidadi.fantasy.common.constant.RedisConstants;
import com.ruoyi.fantasy.common.WorldInit;
import com.ruoyi.fantasy.domain.FantasyCityMapBO;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.fantasy.domain.FantasyItemTypeBO;
import com.ruoyi.fantasy.utils.RedisUtil;
import com.ruoyi.system.domain.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyCitymapMapper;
import com.ruoyi.fantasy.service.IFantasyCitymapService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 城市地点 服务层实现
 * 
 * @author ruoyi
 * @date 2018-12-05
 */
@Service
public class FantasyCitymapServiceImpl implements IFantasyCitymapService 
{
	private static final Logger logger = LoggerFactory.getLogger(FantasyCitymapServiceImpl.class);

	@Resource
	private FantasyCitymapMapper fantasyCitymapMapper;

	@Autowired
	private RedisUtil redisUtil;

	/**
     * 查询城市地点信息
     * 
     * @param id 城市地点ID
     * @return 城市地点信息
     */
    @Override
	public FantasyCityMap selectFantasyCitymapById(String id)
	{
	    return fantasyCitymapMapper.selectFantasyCitymapById(id);
	}
	
	/**
     * 查询城市地点列表
     * 
     * @param fantasyCitymap 城市地点信息
     * @return 城市地点集合
     */
	@Override
	public List<FantasyCityMap> selectFantasyCitymapList(FantasyCityMap fantasyCitymap)
	{
		Map<String, String> cityMaps = redisUtil.hmGet(RedisConstants.FANTASY_CITYMAP_INFO);
		FantasyCityMapBO fantasyCityMapBO = JSONObject.parseObject(cityMaps.get("1"), FantasyCityMapBO.class);
	    return fantasyCitymapMapper.selectFantasyCitymapList(fantasyCitymap);
	}
	
    /**
     * 新增城市地点
     * 
     * @param fantasyCitymap 城市地点信息
     * @return 结果
     */
	@Override
	public int insertFantasyCitymap(FantasyCityMap fantasyCitymap)
	{
		Integer result = fantasyCitymapMapper.insertFantasyCitymap(fantasyCitymap);
		if (result > 0) {
			FantasyCityMapBO fantasyCityMapBO = new FantasyCityMapBO();
			BeanUtils.copyProperties(fantasyCitymap, fantasyCityMapBO);
			redisUtil.hPut(RedisConstants.FANTASY_CITYMAP_INFO, fantasyCityMapBO.getId(), JSON.toJSONString(fantasyCityMapBO));
		}
		return result;
	}

	/**
     * 修改城市地点
     * 
     * @param fantasyCitymap 城市地点信息
     * @return 结果
     */
	@Override
	public int updateFantasyCitymap(FantasyCityMap fantasyCitymap)
	{
		String userId = fantasyCitymap.getUpdateBy();
		//西北
		if (fantasyCitymap.getNorthwest() != null && fantasyCitymap.getNorthwest().equals("-1")) {
			if (fantasyCitymap.getNorthwestInput() != null) {
				FantasyCityMap newNode = new FantasyCityMap();
				newNode.setCityid(fantasyCitymap.getCityid());
				newNode.setPlacename(fantasyCitymap.getNorthwestInput());
				newNode.setPlacedesc("");
				newNode.setSoutheast(fantasyCitymap.getId());
				newNode.setCreateBy(userId);
				newNode.setCreateTime(new Date());
				newNode.setUpdateBy(userId);
				newNode.setUpdateTime(new Date());
				//创建新节点标志
				int flag = -1;
				flag = this.insertFantasyCitymap(newNode);
				if (flag == 1) {
					logger.debug("西北方向新地点id = " + newNode.getId());
					fantasyCitymap.setNorthwest(newNode.getId());
				} else {
					logger.debug("西北方向新地点创建失败！");
				}
			} else {
				logger.debug("西北方向新地点不能为空！");
			}
		}
		//正北
		if (fantasyCitymap.getNorth() != null && fantasyCitymap.getNorth().equals("-1")) {
			if (fantasyCitymap.getNorthInput() != null) {
				FantasyCityMap newNode = new FantasyCityMap();
				newNode.setCityid(fantasyCitymap.getCityid());
				newNode.setPlacename(fantasyCitymap.getNorthInput());
				newNode.setPlacedesc("");
				newNode.setSouth(fantasyCitymap.getId());
				newNode.setCreateBy(userId);
				newNode.setCreateTime(new Date());
				newNode.setUpdateBy(userId);
				newNode.setUpdateTime(new Date());
				//创建新节点标志
				int flag = -1;
				flag = this.insertFantasyCitymap(newNode);
				if (flag == 1) {
					logger.debug("正北方向新地点id = " + newNode.getId());
					fantasyCitymap.setNorth(newNode.getId());
				} else {
					logger.debug("正北方向新地点创建失败！");
				}
			} else {
				logger.debug("正北方向新地点不能为空！");
			}
		}
		//东北
		if (fantasyCitymap.getNortheast() != null && fantasyCitymap.getNortheast().equals("-1")) {
			if (fantasyCitymap.getNortheastInput() != null) {
				FantasyCityMap newNode = new FantasyCityMap();
				newNode.setCityid(fantasyCitymap.getCityid());
				//newNode.setCity(cityMap.getCity());
				newNode.setPlacename(fantasyCitymap.getNortheastInput());
				newNode.setPlacedesc("");
				newNode.setSouthwest(fantasyCitymap.getId());
				newNode.setCreateBy(userId);
				newNode.setCreateTime(new Date());
				newNode.setUpdateBy(userId);
				newNode.setUpdateTime(new Date());
				//创建新节点标志
				int flag = -1;
				flag = this.insertFantasyCitymap(newNode);
				if (flag == 1) {
					logger.debug("东北方向新地点id = " + newNode.getId());
					fantasyCitymap.setNortheast(newNode.getId());
				} else {
					logger.debug("东北方向新地点创建失败！");
				}
			} else {
				logger.debug("东北方向新地点不能为空！");
			}
		}
		//正西
		if (fantasyCitymap.getWest() != null && fantasyCitymap.getWest().equals("-1")) {
			if (fantasyCitymap.getWestInput() != null) {
				FantasyCityMap newNode = new FantasyCityMap();
				newNode.setCityid(fantasyCitymap.getCityid());
				//newNode.setCity(cityMap.getCity());
				newNode.setPlacename(fantasyCitymap.getWestInput());
				newNode.setPlacedesc("");
				newNode.setEast(fantasyCitymap.getId());
				newNode.setCreateBy(userId);
				newNode.setCreateTime(new Date());
				newNode.setUpdateBy(userId);
				newNode.setUpdateTime(new Date());
				//创建新节点标志
				int flag = -1;
				flag = this.insertFantasyCitymap(newNode);
				if (flag == 1) {
					logger.debug("正西方向新地点id = " + newNode.getId());
					fantasyCitymap.setWest(newNode.getId());
				} else {
					logger.debug("正西方向新地点创建失败！");
				}
			} else {
				logger.debug("正西方向新地点不能为空！");
			}
		}
		//正东
		if (fantasyCitymap.getEast() != null && fantasyCitymap.getEast().equals("-1")) {
			if (fantasyCitymap.getEastInput() != null) {
				FantasyCityMap newNode = new FantasyCityMap();
				newNode.setCityid(fantasyCitymap.getCityid());
				//newNode.setCity(cityMap.getCity());
				newNode.setPlacename(fantasyCitymap.getEastInput());
				newNode.setPlacedesc("");
				newNode.setWest(fantasyCitymap.getId());
				newNode.setCreateBy(userId);
				newNode.setCreateTime(new Date());
				newNode.setUpdateBy(userId);
				newNode.setUpdateTime(new Date());
				//创建新节点标志
				int flag = -1;
				flag = this.insertFantasyCitymap(newNode);
				if (flag == 1) {
					logger.debug("正东方向新地点id = " + newNode.getId());
					fantasyCitymap.setEast(newNode.getId());
				} else {
					logger.debug("正东方向新地点创建失败！");
				}
			} else {
				logger.debug("正东方向新地点不能为空！");
			}
		}
		//西南
		if (fantasyCitymap.getSouthwest() != null && fantasyCitymap.getSouthwest().equals("-1")) {
			if (fantasyCitymap.getSouthwestInput() != null) {
				FantasyCityMap newNode = new FantasyCityMap();
				newNode.setCityid(fantasyCitymap.getCityid());
				//newNode.setCity(cityMap.getCity());
				newNode.setPlacename(fantasyCitymap.getSouthwestInput());
				newNode.setPlacedesc("");
				newNode.setNortheast(fantasyCitymap.getId());
				newNode.setCreateBy(userId);
				newNode.setCreateTime(new Date());
				newNode.setUpdateBy(userId);
				newNode.setUpdateTime(new Date());
				//创建新节点标志
				int flag = -1;
				flag = this.insertFantasyCitymap(newNode);
				if (flag == 1) {
					logger.debug("西南方向新地点id = " + newNode.getId());
					fantasyCitymap.setSouthwest(newNode.getId());
				} else {
					logger.debug("西南方向新地点创建失败！");
				}
			} else {
				logger.debug("西南方向新地点不能为空！");
			}
		}
		//正南
		if (fantasyCitymap.getSouth() != null && fantasyCitymap.getSouth().equals("-1")) {
			if (fantasyCitymap.getSouthInput() != null) {
				FantasyCityMap newNode = new FantasyCityMap();
				newNode.setCityid(fantasyCitymap.getCityid());
				//newNode.setCity(cityMap.getCity());
				newNode.setPlacename(fantasyCitymap.getSouthInput());
				newNode.setPlacedesc("");
				newNode.setNorth(fantasyCitymap.getId());
				newNode.setCreateBy(userId);
				newNode.setCreateTime(new Date());
				newNode.setUpdateBy(userId);
				newNode.setUpdateTime(new Date());
				//创建新节点标志
				int flag = -1;
				flag = this.insertFantasyCitymap(newNode);
				if (flag == 1) {
					logger.debug("正南方向新地点id = " + newNode.getId());
					fantasyCitymap.setSouth(newNode.getId());
				} else {
					logger.debug("正南方向新地点创建失败！");
				}
			} else {
				logger.debug("正南方向新地点不能为空！");
			}
		}
		//东南
		if (fantasyCitymap.getSoutheast() != null && fantasyCitymap.getSoutheast().equals("-1")) {
			if (fantasyCitymap.getSoutheastInput() != null) {
				FantasyCityMap newNode = new FantasyCityMap();
				newNode.setCityid(fantasyCitymap.getCityid());
				//newNode.setCity(cityMap.getCity());
				newNode.setPlacename(fantasyCitymap.getSoutheastInput());
				newNode.setPlacedesc("");
				newNode.setNorthwest(fantasyCitymap.getId());
				newNode.setCreateBy(userId);
				newNode.setCreateTime(new Date());
				newNode.setUpdateBy(userId);
				newNode.setUpdateTime(new Date());
				//创建新节点标志
				int flag = -1;
				flag = this.insertFantasyCitymap(newNode);
				if (flag == 1) {
					logger.debug("东南方向新地点id = " + newNode.getId());
					fantasyCitymap.setSoutheast(newNode.getId());
				} else {
					logger.debug("东南方向新地点创建失败！");
				}
			} else {
				logger.debug("东南方向新地点不能为空！");
			}
		}

		Integer result = fantasyCitymapMapper.updateFantasyCitymap(fantasyCitymap);
		if (result > 0) {
			FantasyCityMapBO fantasyCityMapBO = new FantasyCityMapBO();
			BeanUtils.copyProperties(fantasyCitymap, fantasyCityMapBO);
			redisUtil.hPut(RedisConstants.FANTASY_CITYMAP_INFO, fantasyCityMapBO.getId(), JSON.toJSONString(fantasyCityMapBO));
		}
		return result;
	}

	/**
     * 删除城市地点对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFantasyCitymapByIds(String ids)
	{
		Integer result = fantasyCitymapMapper.deleteFantasyCitymapByIds(Convert.toStrArray(ids));
		if (result > 0) {
			redisUtil.hDelete(RedisConstants.FANTASY_CITYMAP_INFO, ids.split(","));
		}
		return result;
	}

	/**
	 * 禁用城市地点对象
	 *
	 * @param ids 需要禁用的数据ID
	 * @return 结果
	 */
	@Override
	public int disableFantasyCitymapByIds(String ids)
	{
		Integer result = fantasyCitymapMapper.disableFantasyCitymapByIds(Convert.toStrArray(ids));
		if (result > 0) {
			redisUtil.hDelete(RedisConstants.FANTASY_CITYMAP_INFO, ids.split(","));
		}
		return result;
	}

	/**
	 * 还原城市地点对象
	 *
	 * @param ids 需要还原的数据ID
	 * @return 结果
	 */
	@Override
	public int restoreFantasyCitymapByIds(String ids)
	{
		Integer result = fantasyCitymapMapper.restoreFantasyCitymapByIds(Convert.toStrArray(ids));
		if (result > 0) {
			String[] idArray = ids.split(",");
			for (int i=0; i<idArray.length; i++) {
				FantasyCityMap fantasyCityMap = fantasyCitymapMapper.selectFantasyCitymapById(idArray[i]);
				FantasyCityMapBO fantasyCityMapBO = new FantasyCityMapBO();
				BeanUtils.copyProperties(fantasyCityMap, fantasyCityMapBO);
				redisUtil.hPut(RedisConstants.FANTASY_CITYMAP_INFO, fantasyCityMapBO.getId(), JSON.toJSONString(fantasyCityMapBO));
			}
		}
		return result;
	}

	@Override
	public void transforData(List<FantasyCityMap> list) {
        Map<String, FantasyCity> citiesMap = WorldInit.citiesMap;
        Map<String, FantasyCityMap> cityMapsMap = WorldInit.cityMapsMap;
		Map<String, SysUser> sysUserMap = WorldInit.sysUserMap;
        for (FantasyCityMap citymap : list) {
			if (citymap.getCityid() != null) {
				if (citiesMap.get(citymap.getCityid()) != null) {
					citymap.setCityname(citiesMap.get(citymap.getCityid()).getCityname());
				} else {
					citymap.setCityname(citymap.getCityid());
				}
			}
			if (citymap.getCreateBy() != null) {
				if (sysUserMap.get(citymap.getCreateBy()) != null) {
					citymap.setCreateBy(sysUserMap.get(citymap.getCreateBy()).getUserName());
				} else {
					citymap.setCreateBy("系统管理员");
				}
			}
			if (citymap.getUpdateBy() != null) {
				if (sysUserMap.get(citymap.getUpdateBy()) != null) {
					citymap.setUpdateBy((sysUserMap.get(citymap.getUpdateBy()).getUserName()));
				} else {
					citymap.setUpdateBy("系统管理员");
				}
			}
			//禁用标记
			if (citymap.getDelFlag().equals("0")) {
				citymap.setDelFlag("正常");
			} else if (citymap.getDelFlag().equals("1")) {
				citymap.setDelFlag("禁用");
			}
		}
	}

}
